<!DOCTYPE html>
<html lang="en">

<!-- Head tag -->
<head><meta name="generator" content="Hexo 3.8.0">

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!--Description-->

    

    
        <meta name="description" content="一、代码YUV420Render的代码参考https://www.cnblogs.com/nanqiang/p/10224891.html，在此表示感谢。
二、应用应用方面，我们以之前的QT播放器为例：
1、获取YUV420数据。注意，这里主要用于h.264解码后为YUV420的视频帧，如果是其他格">
    

    <!--Author-->
    
        <meta name="author" content="Cyrus">
    

    <!--Open Graph Title-->
    
        <meta property="og:title" content="QT OpenGL渲染YUV420格式数据">
    

    <!--Open Graph Description-->
    
        <meta property="og:description" content="一、代码YUV420Render的代码参考https://www.cnblogs.com/nanqiang/p/10224891.html，在此表示感谢。
二、应用应用方面，我们以之前的QT播放器为例：
1、获取YUV420数据。注意，这里主要用于h.264解码后为YUV420的视频帧，如果是其他格">
    

    <!--Open Graph Site Name-->
        <meta property="og:site_name" content="Cyrus的技术空间">

    <!--Type page-->
    
        <meta property="og:type" content="article">
    

    <!--Page Cover-->
    
    
        <meta property="og:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

        <meta name="twitter:card" content="summary_large_image">

    

    
        <meta name="twitter:image" content="http://cyrus_huang.gitee.io/bloghttp://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg">
    

    <!-- Title -->
    
    <title>QT OpenGL渲染YUV420格式数据 - Cyrus的技术空间</title>

    <!-- Bootstrap Core CSS -->
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link rel="stylesheet" href="/blog/css/style.css">

    <!-- Custom Fonts -->
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
    <link href="//fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="//oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->

    <!-- Gallery -->
    <link href="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.css" type="text/css" rel="stylesheet">

    <!-- Google Analytics -->
    


    <!-- favicon -->
    

</head>


<body>

    <!-- Menu -->
    <!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/blog/">Cyrus的技术空间</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                    <li>
                        <a href="/blog/">
                            
                                Home
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/archives">
                            
                                Archives
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/tags">
                            
                                Tags
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="/blog/categories">
                            
                                Categories
                            
                        </a>
                    </li>
                
                    <li>
                        <a href="https://github.com/klugjo/hexo-theme-clean-blog">
                            
                                <i class="fa fa-github fa-stack-2x"></i>
                            
                        </a>
                    </li>
                
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

    <!-- Main Content -->
    <!-- Page Header -->
<!-- Set your background image for this header in your post front-matter: cover -->

<header class="intro-header" style="background-image: url('http://www.codeblocq.com/assets/projects/hexo-theme-clean-blog/img/home-bg.jpg')">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <div class="post-heading">
                    <h1>QT OpenGL渲染YUV420格式数据</h1>
                    
                    <span class="meta">
                        <!-- Date and Author -->
                        
                            Posted by Cyrus on
                        
                        
                            2019-10-06
                        
                    </span>
                </div>
            </div>
        </div>
    </div>
</header>

<!-- Post Content -->
<article>
    <div class="container">
        <div class="row">

            <!-- Tags and categories -->
           
                <div class="col-lg-4 col-lg-offset-2 col-md-5 col-md-offset-1 post-tags">
                    
                        


<a href="/blog/tags/QT/">#QT</a>


                    
                </div>
                <div class="col-lg-4 col-md-5 post-categories">
                    
                        

<a href="/blog/categories/OpenGL/">OpenGL</a>/ <a href="/blog/categories/OpenGL/播放器/">播放器</a>

                    
                </div>
            

            <!-- Gallery -->
            

            <!-- Post Main Content -->
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <h3 id="一、代码"><a href="#一、代码" class="headerlink" title="一、代码"></a>一、代码</h3><p>YUV420Render的代码参考<a href="https://www.cnblogs.com/nanqiang/p/10224891.html，在此表示感谢。" target="_blank" rel="noopener">https://www.cnblogs.com/nanqiang/p/10224891.html，在此表示感谢。</a></p>
<h3 id="二、应用"><a href="#二、应用" class="headerlink" title="二、应用"></a>二、应用</h3><p>应用方面，我们以之前的QT播放器为例：</p>
<h4 id="1、获取YUV420数据。注意，这里主要用于h-264解码后为YUV420的视频帧，如果是其他格式的，建议转成rgb后显示（QT-OpenGL渲染rgb的下一篇讲）。"><a href="#1、获取YUV420数据。注意，这里主要用于h-264解码后为YUV420的视频帧，如果是其他格式的，建议转成rgb后显示（QT-OpenGL渲染rgb的下一篇讲）。" class="headerlink" title="1、获取YUV420数据。注意，这里主要用于h.264解码后为YUV420的视频帧，如果是其他格式的，建议转成rgb后显示（QT OpenGL渲染rgb的下一篇讲）。"></a>1、获取YUV420数据。注意，这里主要用于h.264解码后为YUV420的视频帧，如果是其他格式的，建议转成rgb后显示（QT OpenGL渲染rgb的下一篇讲）。</h4><p>代码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line">// 注意，这里默认_videoCodecCtx-&gt;pix_fmt == AV_PIX_FMT_YUV420P || _videoCodecCtx-&gt;pix_fmt == AV_PIX_FMT_YUVJ420P，使用YUV的显示的话，就不用设置SwsContext转格式了</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">MPVideoFrame *MPMovieDecoder::handleVideoFrame()</span><br><span class="line">&#123;</span><br><span class="line">    qDebug() &lt;&lt; &quot;MPMovieDecoder::handleVideoFrame()&quot; &lt;&lt; endl;</span><br><span class="line">    if (_videoFrame-&gt;data[0] == NULL) &#123;</span><br><span class="line">        qDebug() &lt;&lt; &quot;_videoFrame-&gt;data[0] no data&quot; &lt;&lt; endl;</span><br><span class="line">        return NULL;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    MPVideoFrame *frame = new MPVideoFrame();</span><br><span class="line">    frame-&gt;type = MPMovieFrameTypeVideo;</span><br><span class="line"></span><br><span class="line">    int lineSize = _videoFrame-&gt;linesize[0] &gt; _videoCodecCtx-&gt;width ? _videoCodecCtx-&gt;width : _videoFrame-&gt;linesize[0];</span><br><span class="line">    qDebug() &lt;&lt; &quot;linesize:&quot; &lt;&lt; lineSize &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">	// 这里把yuv的数据放在一起的方式，主要是为了兼容rgb的渲染，详见下一篇</span><br><span class="line">    int size = lineSize * _videoCodecCtx-&gt;height;</span><br><span class="line">    frame-&gt;databuffer = (unsigned char *)malloc(size * 3 / 2);</span><br><span class="line">    frame-&gt;bufferSize = size * 3 / 2;</span><br><span class="line">    copyFrameData(_videoFrame-&gt;data[0], frame-&gt;databuffer, _videoFrame-&gt;linesize[0], _videoCodecCtx-&gt;width, _videoCodecCtx-&gt;height);</span><br><span class="line">    copyFrameData(_videoFrame-&gt;data[1], frame-&gt;databuffer + size, _videoFrame-&gt;linesize[1], _videoCodecCtx-&gt;width/2, _videoCodecCtx-&gt;height/2);</span><br><span class="line">    copyFrameData(_videoFrame-&gt;data[2], frame-&gt;databuffer + size * 5 / 4, _videoFrame-&gt;linesize[2], _videoCodecCtx-&gt;width/2, _videoCodecCtx-&gt;height/2);</span><br><span class="line"></span><br><span class="line">    frame-&gt;width = _videoFrame-&gt;lineSize;</span><br><span class="line">    frame-&gt;height = _videoCodecCtx-&gt;height;</span><br><span class="line">    qDebug() &lt;&lt; &quot;frame-&gt;width:&quot; &lt;&lt; frame-&gt;width &lt;&lt;  &quot; frame-&gt;height:&quot; &lt;&lt; frame-&gt;height &lt;&lt; endl;</span><br><span class="line">    frame-&gt;position = av_frame_get_best_effort_timestamp(_videoFrame) * _videoTimeBase;</span><br><span class="line"></span><br><span class="line">    const int64_t frameDuration = av_frame_get_pkt_duration(_videoFrame);</span><br><span class="line">    if (frameDuration) &#123;</span><br><span class="line">        frame-&gt;duration = frameDuration * _videoTimeBase;</span><br><span class="line">        frame-&gt;duration += _videoFrame-&gt;repeat_pict * _videoTimeBase * 0.5;</span><br><span class="line">    &#125; else &#123;</span><br><span class="line"></span><br><span class="line">        // sometimes, ffmpeg unable to determine a frame duration</span><br><span class="line">        // as example yuvj420p stream from web camera</span><br><span class="line">        frame-&gt;duration = 1.0 / fps;</span><br><span class="line">    &#125;</span><br><span class="line">    return frame;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">// 拷贝数据</span><br><span class="line">void MPMovieDecoder::copyFrameData(unsigned char *src, unsigned char *ioData, int linesize, int width, int height)</span><br><span class="line">&#123;</span><br><span class="line">    width = width &gt; linesize ? linesize : width;</span><br><span class="line">    int dst = 0;</span><br><span class="line">    for (int i = 0; i &lt; height; i++) &#123;</span><br><span class="line">        memcpy(ioData, src, width);</span><br><span class="line">        src += linesize;</span><br><span class="line">        ioData += width;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="2、播放"><a href="#2、播放" class="headerlink" title="2、播放"></a>2、播放</h4><p>代码：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line">// MyOpenGLWidget.h</span><br><span class="line"></span><br><span class="line">#ifndef MYOPENGLWIDGET_H</span><br><span class="line">#define MYOPENGLWIDGET_H</span><br><span class="line"></span><br><span class="line">#include &lt;QObject&gt;</span><br><span class="line">#include &lt;QOpenGLWidget&gt;</span><br><span class="line">#include &lt;QOpenGLFunctions&gt;</span><br><span class="line">#include &lt;QOpenGLShaderProgram&gt;</span><br><span class="line">#include &quot;yuv420_render.h&quot;</span><br><span class="line">#include &quot;mpvideoframe.h&quot;</span><br><span class="line"></span><br><span class="line">class MyOpenGLWidget : public QOpenGLWidget</span><br><span class="line">&#123;</span><br><span class="line">    Q_OBJECT</span><br><span class="line">public:</span><br><span class="line">    explicit MyOpenGLWidget(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());</span><br><span class="line">    void setFrame(MPVideoFrame *frame);</span><br><span class="line"></span><br><span class="line">protected:</span><br><span class="line"></span><br><span class="line">    void initializeGL();</span><br><span class="line">    void paintGL();</span><br><span class="line">    void resizeGL(int w, int h);</span><br><span class="line"></span><br><span class="line">private:</span><br><span class="line">    YUV420_Render *pRender;</span><br><span class="line">    MPVideoFrame  *pCurrentFrame;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">#endif // MYOPENGLWIDGET_H</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">// MyOpenGLWidget.cpp</span><br><span class="line">#include &quot;myopenglwidget.h&quot;</span><br><span class="line">#include &quot;rgb_render.h&quot;</span><br><span class="line"></span><br><span class="line">MyOpenGLWidget::MyOpenGLWidget(QWidget *parent, Qt::WindowFlags f) : QOpenGLWidget(parent, f)</span><br><span class="line">&#123;</span><br><span class="line">    pRender = new YUV420_Render();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void MyOpenGLWidget::initializeGL()</span><br><span class="line">&#123;</span><br><span class="line">    pCurrentFrame = NULL;</span><br><span class="line">    pRender-&gt;initialize();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void MyOpenGLWidget::setFrame(MPVideoFrame *frame)</span><br><span class="line">&#123;</span><br><span class="line">    if(this-&gt;pCurrentFrame != NULL) &#123;</span><br><span class="line">        delete this-&gt;pCurrentFrame;</span><br><span class="line">    &#125;</span><br><span class="line">    this-&gt;pCurrentFrame = frame;</span><br><span class="line">    update();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void MyOpenGLWidget::paintGL()</span><br><span class="line">&#123;</span><br><span class="line">    if(pCurrentFrame == NULL) &#123;</span><br><span class="line">        return;</span><br><span class="line">    &#125;</span><br><span class="line">//    pRender-&gt;render(pCurrentFrame-&gt;yData, pCurrentFrame-&gt;uData, pCurrentFrame-&gt;vData, pCurrentFrame-&gt;width, pCurrentFrame-&gt;height, 0);</span><br><span class="line">    pRender-&gt;render(pCurrentFrame-&gt;databuffer, pCurrentFrame-&gt;width, pCurrentFrame-&gt;height, 0);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">void MyOpenGLWidget::resizeGL(int w, int h)</span><br><span class="line">&#123;</span><br><span class="line">    glViewport(0, 0, w, h);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>MainWidget里面，将之前的QLabel改为MyOpenGLWidght<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">double MainWindow::presentVideoFrame(MPVideoFrame *frame)</span><br><span class="line">&#123;</span><br><span class="line">//    displayRBGFrame((char *)frame-&gt;databuffer, frame-&gt;width, frame-&gt;height);</span><br><span class="line"></span><br><span class="line">	// 直接调用setFrame就可以显示</span><br><span class="line">    ui-&gt;openGLWidget-&gt;setFrame(frame);</span><br><span class="line">    _moviePosition = frame-&gt;position;</span><br><span class="line">    return frame-&gt;duration;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>


                
            </div>

            <!-- Comments -->
            
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    


                </div>
            
        </div>
    </div>
</article>

    <!-- Footer -->
    <hr>

<!-- Footer -->
<footer>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                <ul class="list-inline text-center">
                    

                    

                    
                        <li>
                            <a href="https://github.com/klugjo/hexo-theme-clean-blog" target="_blank">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    

                    

                    

                    
                </ul>
                <p class="copyright text-muted">&copy; 2020 Cyrus<br></p>
                <p class="copyright text-muted">Original Theme <a target="_blank" href="http://startbootstrap.com/template-overviews/clean-blog/">Clean Blog</a> from <a href="http://startbootstrap.com/" target="_blank">Start Bootstrap</a></p>
                <p class="copyright text-muted">Adapted for <a target="_blank" href="https://hexo.io/">Hexo</a> by <a href="http://www.codeblocq.com/" target="_blank">Jonathan Klughertz</a></p>
                <p class="copyright text-muted"><a href="http://www.beian.miit.gov.cn">粤ICP备18110122号-1</a></p>
            </div>
        </div>
    </div>
</footer>


    <!-- After footer scripts -->
    
<!-- jQuery -->
<script src="//code.jquery.com/jquery-2.1.4.min.js"></script>

<!-- Bootstrap -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<!-- Gallery -->
<script src="//cdnjs.cloudflare.com/ajax/libs/featherlight/1.3.5/featherlight.min.js" type="text/javascript" charset="utf-8"></script>

<!-- Disqus Comments -->



</body>

</html>